package com.study.portal.controller.action;

import java.io.IOException;
import java.util.List;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionContext;
import com.study.portal.pojo.system.Nav;
import com.study.portal.service.INavService;
import com.study.portal.util.StrutsUtil;

@SuppressWarnings("serial")
public class IndexAction extends ActionSupport {

    private INavService navService;
    private Nav searchFrom;
    private String requested = "NOT";

    public String getRequested() {
        return requested;
    }

    public void setRequested(String requested) {
        this.requested = requested;
    }

    public void setNavService(INavService navService) {
        this.navService = navService;
    }

    public Nav getSearchFrom() {
        return searchFrom;
    }

    public void setSearchFrom(Nav searchFrom) {
        this.searchFrom = searchFrom;
    }

    public String index() {
        requested = "YES";

        // 获取首页导航
        List<Nav> navs = getIndexNav();
        ActionContext act = ActionContext.getContext();
        act.put("navs", navs);
        return INDEX;
    }
    
    private List<Nav> getIndexNav(){
        // 获取首页导航
        Nav nav = new Nav();// 重新获取导航
        nav.setIsUse(Nav.USER_Y);
        List<Nav> navs = this.navService.select(nav);
        return navs;
    }
    
    /**
     * 获取导航的Ajax方法
     * @return
     * @throws IOException 
     */
    public String getIndexNavs$() throws IOException{
        List<Nav> navs = getIndexNav();
        StrutsUtil.write(JSONArray.fromObject(navs).toString());
        
        String json = JSONArray.fromObject(navs).toString();
        System.out.println(json);
        ActionContext.getContext().getSession().put("json", json);
        System.out.println(json);
        return null;
    }
}
